#include <iostream>
#include <vector>
#include <numeric>   // for std::accumulate
#include <cmath>     // for std::abs
#include <algorithm> // for std::min
#include <limits>    // for std::numeric_limits

using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int>> matrix(n, vector<int>(m));
    int total_sum = 0;
    vector<int> col_sums(m, 0);
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cin >> matrix[i][j];
            total_sum += matrix[i][j];
        }
    }
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < n; j++)
        {
            col_sums[i] += matrix[j][i];
        }
    }

    int min_diff = numeric_limits<int>::max();

    int left_sum = 0;
    for(int j = 0; j < m; j++)
    {
        left_sum += col_sums[j];
        int diff = abs(left_sum - (total_sum - left_sum));
        min_diff = min(min_diff, diff);
    }

    vector<int> row_sums(n, 0);
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            row_sums[i] += matrix[i][j];
        }
    }

    int top_sum = 0;
    for(int i = 0; i < n; i++)
    {
        top_sum += row_sums[i];
        int diff = abs(top_sum - (total_sum - top_sum));
        min_diff = min(min_diff, diff);
    }

    cout << min_diff << endl;
    return 0;
}